const { ccclass, property } = cc._decorator;
import s from "./frameworkManager"
import u from "./EngineUtil"
import p from "./MathUtil"
import d, { MatchGameStatus } from "./MatchGameData"
import h from "./MatchGameConfig"
import f, { LayerType, ObstacleAllType } from "./ObstacleConfig"
import g from "./MatchGameDataMgr"
import y from "./SquareStatusMgr"
import m from "./BottomBgCtrl"
import _ from "./FlyPropellerCtrl"
import v from "./MatchItemCtrl"
import b from "./ObstacleCtrl"
import C from "./PropsCtrl"
import T from "./MatchEffectCtrl"
import M from "./UiManage"
import P from "./MatchGameUtilsDataMgr"
import S from "./MatchGameCenter"
import w from "./MatchAniCtrl"
import A from "./ObstacleAniCtrl"
import O from "./PropAniCtrl"
import I from "./SkillAniCtrl"
import N from "./TargetAniCtrl"
import E from "./MatchGameViewCtrl"
import UiManager from "./UiManage";
import MatchGameConfig from "./MatchGameConfig";
import MathUtils from "./MathUtil";

@ccclass
export default class MatchGameCenterCtrl extends cc.Component {
    public bottomBgParent = null;
    public lowestParent = null;
    public middleParents = null;
    public middleMaskParents = null;
    public topParents = null;
    public bottomPrefab = null;
    public matchItemPrefab = null;
    public propItemPrefab = null;
    public obstacleItemPrefab = null;
    public emptyObstaclePrefab = null;
    public flyPropellerPrefab = null;
    public flyPropellerParent = null;
    public effectParent = null;
    public skilltParent = null;
    public multiAreaParent = null;
    public multiObstacleItem = null;
    public cupboardPlate = null;
    public colorBottle = null;
    public curtain = null;
    public flyMailPrefab = null;
    public safeBoxPrefab = null;
    public grassLandPrefab = null;
    public icicle = null;
    public matchDestoryPrefab = null;
    public matchEffectPrefab = null;
    public honeyPrefab = null;
    public birdNestPrefab = null;
    public cupHolderPrefab = null;
    public candleboxPrefab = null;
    public drillFillPrefab = null;
    public bottomSquareGrid = [];
    public topSquareGrid = [];
    public displayMapInfo = [];
    public _bottomArr = [];
    public _map_row = h.MAX_ROW;
    public _map_col = h.MAX_COL;
    public _mapWidth = MatchGameConfig.ITEM_WIDTH * h.MAX_ROW;
    public _mapHeight = MatchGameConfig.ITEM_HEIGHT * h.MAX_COL;
    public curLevelMaxCol = 0;
    public curLevelMaxRow = 0;
    public initGameInfo = null;
    public fixedDropData = [];
    public fixDropIndexArr = [];
    public probabilityDropData = [];
    public maxFixedDropCount = 0;
    public allBottomBgMaskArr = [];
    public allpolygons = [];
    public testMaskPrefab = null;
    public guideGridPrefab = null;
    public guideGridParent = null;
    public nextDropStr = null;
    public dropInfo = [];

    public ui;

    static _instance: MatchGameCenterCtrl;
    public async onLoad() {
        MatchGameCenterCtrl._instance = this;
        this.onUILoad();
        this.addButtonListen();

    };
    public onUILoad() {
        this.ui = this.node.addComponent(S);
    };
    public addButtonListen() { };
    public resetFixedDropData() { };
    public initFixedDropData(e) {
        this.fixedDropData = [];
        var t = e;
        t.reverse();
        for (var a = 0, o = 0; o < t.length; o++) a = Math.max(a, t[o].length);
        for (o = 0; o < a; o++) this.fixedDropData[o] = [];
        for (o = 0; o < t.length; o++) for (var i = 0; i < t[o].length; i++) this.fixedDropData[i].push(t[o][i]);
        this.maxFixedDropCount = 0;
        for (o = 0; o < this.fixedDropData.length; o++) this.maxFixedDropCount = Math.max(this.maxFixedDropCount, this.fixedDropData[o].length);
        s.log("初始化固定刷新数组-----", this.fixedDropData, this.maxFixedDropCount);
        this.initDropIndexArr();
        this.logLeaveObstaceCount();
    };
    public initDropIndexArr() {
        this.fixDropIndexArr = [];
        if (this.fixedDropData.length) for (var e = 0; e < this.fixedDropData.length; e++) this.fixDropIndexArr[e] = 0;
    };
    public addDropIndex(e) {
        this.fixDropIndexArr[e] += 1;
        this.logLeaveObstaceCount();
    };
    public logLeaveObstaceCount() {
        for (var e = 0, t = 0; t < this.fixedDropData.length; t++) for (var a = 0; a < this.fixedDropData[t].length; a++) this.fixDropIndexArr[t] <= a && "N" !== this.fixedDropData[t][a] && e++;
        s.log("logLeaveObstaceCount---", e, this.fixDropIndexArr, this.fixedDropData);
    };
    public getDropIndexByRow(e) {
        return this.fixDropIndexArr[e];
    };
    public getFixedDropByRow(e) {
        if (!this.fixedDropData.length) return null;
        var t = this.fixDropIndexArr[e];
        if (t > this.maxFixedDropCount) return null;
        var a = this.fixedDropData[e][t];
        return a == h.SquareType.Null || a == h.SquareType.EMPTY ? null : a;
    };
    public createDropSquare(e, t) {
        var a;
        if (d.gameStatus == MatchGameStatus.DuringGame) {
            if (1 !== d.backend_level_flag && this.nextDropStr) {
                a = this.nextDropStr;
                s.log("走了下落兜底刷新", a);
                this.nextDropStr = null;
            }
            a || (a = this.getFixedDropByRow(e));
            a || (a = this.getRandomProbabilityData().squareTypeStr);
            this.addDropIndex(e);
        } else a = "M-" + this.getNoEliminateDropMatchType(e, t);
        return this.createMiddleSquareItem(e, t, a, true);
    };
    public getSquareProbabilityDropArr() {
        for (var e = [], t = 0; t < this.probabilityDropData.length; t++) {
            var a = this.probabilityDropData[t], o = a.squareTypeStr;
            if (!(a.precentNumber <= 0)) {
                var i = o.split("-");
                i[0] == h.SquareType.MatchItem && e.push(Number(i[1]));
            }
        }
        return e;
    };
    public getAllMatchTypeCountArr() {
        var e = [];
        e[0] = [];
        for (var t = 1; t <= h.MAX_MATCH_TYPE_COUNT; t++) e[t] = [];
        for (var a = 0; a < this._map_row; a++) for (var o = 0; o < this._map_col; o++) if (g.middleSquareGrid[a] && g.middleSquareGrid[a][o]) {
            var i = g.middleSquareGrid[a][o];
            i && i.node && i.itemType && i.squareType === h.SquareType.MatchItem && !y.getSquareDestoryStatus(i) && !y.getSquareReadyDestoryStatus(i) && e[i.itemType].push(i);
        }
        var r = {
            id: h.MatchItemType.null,
            count: 0
        };
        for (t = 1; t <= h.MAX_MATCH_TYPE_COUNT; t++) e[t].length > r.count && (r = {
            id: t,
            count: e[t].length
        });
        return r;
    };
    public getAllTargetDropStr(e) {
        for (var t = 0; t < e.length; t++) {
            var a = this.getCreateTargetDrop(e[t]);
            if (a) return a;
        }
        return null;
    };
    public getCreateTargetDrop(e) {
        var t = e.squareType, a = e.itemType, o = e.tootalCount, i = (e.clearCount, e.isFinish), r = d.getSquareStrType(t);
        if (r !== h.SquareType.ObstacleItem) return null;
        if (i) return null;
        if (a !== ObstacleAllType.Egg && a !== ObstacleAllType.Vase && a !== ObstacleAllType.Oyster && a !== ObstacleAllType.Rock && a !== ObstacleAllType.Flowerpot && a !== ObstacleAllType.PorcelainPig && a !== ObstacleAllType.Bird) return null;
        var n = o, l = b.getObstacleCountInScene(a);
        s.log("目标值剩余数量", o);
        s.log("场景中剩余数量", l);
        if (l >= n) return null;
        for (var c = r + "-" + a, u = 0; u < this.probabilityDropData.length; u++) {
            var p = this.probabilityDropData[u], g = p.squareTypeStr;
            p.precentNumber;
            if (-1 !== g.indexOf(c)) return null;
        }
        if (!this.fixedDropData.length) return c + "_1";
        for (u = 0; u < this.fixedDropData.length; u++) for (var y = 0; y < this.fixedDropData[u].length; y++) if (-1 !== this.fixedDropData[u][y].indexOf(c) && this.fixDropIndexArr[u] <= y) return null;
        s.log("兜底都住了", c + "_1");
        return c + "_1";
    };
    public getNoEliminateDropMatchType(e, t) {
        var a = this.getSquareProbabilityDropArr();
        s.log("getNoEliminateDropMatchType probDropArr", a);
        for (var o = [], i = -1; i <= 1; i++) for (var r = -1; r <= 1; r++) if (i !== r && i + r !== 0) {
            var n = this.getMatchType(e + i, t + r);
            n && o.push(n);
        }
        s.log("createNoEliminateDropMatch arroundMatchTypeArr", o);
        var l = [];
        if (o.length) for (var c = 0; c < a.length; c++) -1 == o.indexOf(a[c]) && l.push(a[c]);
        return l.length ? l[u.getRandomNumber(0, l.length)] : a[u.getRandomNumber(0, a.length)];
    };
    public getMatchType(e, t) {
        return g.middleSquareGrid[e] && g.middleSquareGrid[e][t] && g.middleSquareGrid[e][t].squareType == h.SquareType.MatchItem ? g.middleSquareGrid[e][t].itemType : null;
    };
    public initProbabilityDropData(e) {
        e.forEach((e) => {
            e.squareType && (e.squareTypeStr = d.getSquareStrType(e.squareType) + "-" + e.itemType);
        });
        for (var t = [], a = 0, o = 0; o < e.length; o++) {
            var i = e[o];
            if (i.precentNumber > 0) {
                a += i.precentNumber;
                i.precentIndex = a;
                t.push(i);
            }
        }
        this.probabilityDropData = t;
        s.log("this.probabilityDropData---", this.probabilityDropData);
        return this.probabilityDropData;
    };
    public getRandomProbabilityData() {
        for (var e = u.getRandomNum(0, 100), t = 0; t < this.probabilityDropData.length; t++) if (this.probabilityDropData[t].precentIndex > e) return this.probabilityDropData[t];
        s.log("获取兜底刷新报错了？？？", e);
        return this.probabilityDropData[0];
    };
    public resetData() {
        g.resetData();
        g.middleSquareGrid = [];
        this.bottomSquareGrid = [];
        this.topSquareGrid = [];
        this.displayMapInfo = [];
        this._bottomArr = [];
        this.curLevelMaxCol = 0;
        this.curLevelMaxRow = 0;
        this.bottomBgParent = this.ui.bottomBgParent;
        this.lowestParent = this.ui.LowestParent;
        this.middleParents = this.ui.MiddleParents;
        this.middleMaskParents = this.ui.MiddleDropMask;
        this.topParents = this.ui.TopParents;
        this.flyPropellerParent = this.ui.flyPropellerParent;
        this.effectParent = this.ui.effectParent;
        this.skilltParent = this.ui.skilltParent;
        this.multiAreaParent = this.ui.multiAreaParent;
        this.ui.LightingBallPlayingBg.active = false;
        this.guideGridParent = this.ui.guideGridParent;
        this.guideGridParent.removeAllChildren();
        E.instance.blockEliminate(true);
        this.lowestParent.removeAllChildren();
        this.middleParents.removeAllChildren();
        this.middleMaskParents.removeAllChildren();
        this.topParents.removeAllChildren();
        this.flyPropellerParent.removeAllChildren();
        this.effectParent.removeAllChildren();
        this.multiAreaParent.removeAllChildren();
        this.skilltParent.removeAllChildren();
    };
    public async initData(e) {
        this.resetData();
        s.log("游戏初始化----", e);
        this.initGameInfo = e;


        if (!this.matchItemPrefab) {
            const prefabPaths = [
                "prefabs/matchItemPrefab",
                "prefabs/propPrefab",
                "prefabs/obstaclePrefab",
                "prefabs/EmptyObstacleItem",
                "prefabs/flyPropellerPrefab",
                "prefabs/multiObstaclePrefab",
                "prefabs/CupboardPlate",
                "prefabs/ColorBottle",
                "prefabs/Curtain",
                "prefabs/flyMailPrefab",
                "prefabs/SafeBox",
                "prefabs/GrassLand",
                "prefabs/bottomPrefab",
                "prefabs/Icicle",
                "prefabs/matchDestoryItem",
                "prefabs/matchMatchEffectItem",
                "prefabs/HoneyPrefab",
                "prefabs/redPointPrefab",
                "prefabs/BirdNest",
                "prefabs/cupHolderPrefab",
                "prefabs/Candlebox",
                "prefabs/guideGridPrefab",
                "prefabs/DrillFill"
            ];

            const prefabs = await Promise.all(
                prefabPaths.map(path => UiManager.loaderPrefabInDeepPath(path))
            );

            [
                this.matchItemPrefab,
                this.propItemPrefab,
                this.obstacleItemPrefab,
                this.emptyObstaclePrefab,
                this.flyPropellerPrefab,
                this.multiObstacleItem,
                this.cupboardPlate,
                this.colorBottle,
                this.curtain,
                this.flyMailPrefab,
                this.safeBoxPrefab,
                this.grassLandPrefab,
                this.bottomPrefab,
                this.icicle,
                this.matchDestoryPrefab,
                this.matchEffectPrefab,
                this.honeyPrefab,
                this.testMaskPrefab,
                this.birdNestPrefab,
                this.cupHolderPrefab,
                this.candleboxPrefab,
                this.guideGridPrefab,
                this.drillFillPrefab
            ] = prefabs;
        }

        await Promise.all([
            O.init(),
            I.init(),
            A.init(),
            N.init(),
            w.init()
        ]);

        const promises = [
            m.ready(this.bottomPrefab, this.bottomBgParent),
            _.ready(this.flyPropellerPrefab, this.flyPropellerParent),
            s.log("游戏初始化2----"),
            v.ready(this.matchItemPrefab, this.middleParents, this.middleMaskParents),
            b.ready(this.obstacleItemPrefab, this.emptyObstaclePrefab, this.lowestParent, this.middleParents, this.topParents, this.multiObstacleItem),
            C.ready(this.propItemPrefab, this.middleParents, this.middleMaskParents),
            T.ready(this.effectParent),
            w.ready(this.effectParent, this.matchDestoryPrefab, this.matchEffectPrefab),
            O.ready(this.skilltParent),
            I.ready(this.skilltParent),
            A.ready(this.effectParent, E.instance.ui.targetEffectParent),
            N.ready(E.instance.ui.targetEffectParent)
        ];

        await Promise.all(promises);

        this.startGame();

        s.log("游戏初始化3----");

    };
    public start() { };
    public startGame() {
        this.allBottomBgMaskArr = [];
        this.nextDropStr = null;
        this.allpolygons = [];
        s.log("this.initGameInfo.middleSquareData----", this.initGameInfo.middleSquareData);
        this._initSquareMap();
        this.loadDisplayMapData(this.initGameInfo.mapArr);
        this.loadBottomData(this.initGameInfo.bottomSquareData);
        this.setNoticeParentPos();
        this.loadMiddleData(this.initGameInfo.middleSquareData);
        this.loadTopData(this.initGameInfo.topSquareData);
        this.initFixedDropData(this.initGameInfo.fixedDropData);
        this.initProbabilityDropData(this.initGameInfo.probabilityDropData);
        g.init();
        b.init(this.initGameInfo);
        d.initTargetData();
        this.initBottomMaskArr();
        this.setAllPolygon();
    };
    public setDuringGameStatu() {
        d.gameStatus = MatchGameStatus.DuringGame;
    };
    public _initSquareMap() {
        for (var e = 0; e < h.MAX_ROW; e++) {
            g.middleSquareGrid[e] = [];
            this.bottomSquareGrid[e] = [];
            this.displayMapInfo[e] = [];
            this.topSquareGrid[e] = [];
            for (var t = 0; t < h.MAX_COL; t++) {
                g.middleSquareGrid[e][t] = null;
                this.bottomSquareGrid[e][t] = null;
                this.displayMapInfo[e][t] = null;
                this.topSquareGrid[e][t] = null;
            }
        }
    };
    public initBottomMaskArr() {
        for (var e = 0; e < h.MAX_ROW; e++) for (var t = 0; t < h.MAX_COL; t++) this.initOneBottomMaskArr(e, t, this.displayMapInfo);
    };
    public initOneBottomMaskArr(e, t, o) {
        if (this.displayMapInfo[e][t] != h.DisplayMapType.EmptyBlock) {
            var i = [], r = [], n = [], l = [], s = [], c = [], u = cc.v2(0, -78), p = cc.v2(0, 0), d = cc.v2(78, 0), f = cc.v2(78, -78), g = P.getPos(e, t);
            if (0 == e || o[e - 1][t] == h.DisplayMapType.EmptyBlock) {
                n.push(cc.v2(u.x + g.x, u.y + g.y));
                n.push(cc.v2(p.x + g.x, p.y + g.y));
            }
            if (0 == t || o[e][t - 1] == h.DisplayMapType.EmptyBlock) {
                l.push(cc.v2(p.x + g.x, p.y + g.y));
                l.push(cc.v2(d.x + g.x, d.y + g.y));
            }
            if (e == o.length - 1 || o[e + 1][t] == h.DisplayMapType.EmptyBlock) {
                s.push(cc.v2(d.x + g.x, d.y + g.y));
                s.push(cc.v2(f.x + g.x, f.y + g.y));
            }
            if (t == o[e].length - 1 || o[e][t + 1] == h.DisplayMapType.EmptyBlock) {
                c.push(cc.v2(f.x + g.x, f.y + g.y));
                c.push(cc.v2(u.x + g.x, u.y + g.y));
            }
            if (n.length || s.length) if (l.length || c.length) {
                if (l.length) if (s.length) {
                    n.length && (i = i.concat(n));
                    l.length && (i = i.concat(l));
                    s.length && (i = i.concat(s));
                    c.length && (i = i.concat(c));
                } else {
                    c.length && (i = i.concat(c));
                    n.length && (i = i.concat(n));
                    l.length && (i = i.concat(l));
                } else {
                    s.length && (i = i.concat(s));
                    c.length && (i = i.concat(c));
                    n.length && (i = i.concat(n));
                }
                if (0 == i.length) return null;
                for (var y = 0; y < i.length; y++) for (var m = y + 1; m < i.length; m++) if (i[y].x == i[m].x && i[y].y == i[m].y) {
                    i.splice(m, 1);
                    m--;
                }
            } else {
                i = i.concat(n);
                r = r.concat(s);
            } else {
                i = i.concat(l);
                r = r.concat(c);
            }
            i.length && MatchGameCenterCtrl._instance.allBottomBgMaskArr.push(i);
            r.length && MatchGameCenterCtrl._instance.allBottomBgMaskArr.push(r);
        }
    };
    public setAllPolygon() {
        0 == this.allpolygons.length && (this.allpolygons = this.allBottomBgMaskArr.shift());
        for (var e = this.allpolygons[this.allpolygons.length - 1], t = false, a = 0; a < this.allBottomBgMaskArr.length; a++) {
            var o = this.allBottomBgMaskArr[a][0];
            if (MathUtils.getDistance(cc.v3(e), cc.v3(o)) < 20) {
                t = true;
                var i = this.allBottomBgMaskArr.splice(a, 1), r = this.allpolygons.pop();
                i[0].splice(1, 0, r);
                this.allpolygons = this.allpolygons.concat(i[0]);
                this.setAllPolygon();
                return;
            }
        }
        if (!t) {
            var n = this.allpolygons[0];
            this.allpolygons.push(n);
            if (this.allBottomBgMaskArr.length > 0) {
                s.log("去找下一个遮罩区域", this.allBottomBgMaskArr.length, this.allpolygons.length);
                this.allpolygons = this.allpolygons.concat(this.allBottomBgMaskArr.shift());
                this.setAllPolygon();
            } else {
                s.log("已经找到所有点", this.allpolygons);
                this.initMask(this.allpolygons);
            }
        }
    };
    public initMask(e) {
        var t = this;
        this.scheduleOnce(function () {
            // t.middleMaskParents.getComponent(CCGraphicsMask).enabled = true;
            // t.middleMaskParents.getComponent(CCGraphicsMask).addPolygon("center_mask", cc.v2(), e);
            // t.middleMaskParents.getComponent(CCGraphicsMask).showPolygon("center_mask");
        });
    };
    public loadBottomData(e) {
        this._map_row = e.length;
        this._map_col = e[0].length;
        for (var t = 0; t < this._map_row; t++) for (var a = 0; a < this._map_col; a++) {
            m.initBottomBgItem(t, a, this.displayMapInfo);
            if (this.displayMapInfo[t][a] === h.DisplayMapType.Normal) {
                this.curLevelMaxCol = Math.max(this.curLevelMaxCol, a);
                this.curLevelMaxRow = Math.max(this.curLevelMaxRow, t);
                this.createBottomSquareItem(t, a, e[t][a]);
            }
        }
        console.log("curLevelMaxCol--", this.curLevelMaxCol, this.curLevelMaxRow);
    };
    public setNoticeParentPos() {
        this.ui.MatchGameCenter.y, this.curLevelMaxCol, MatchGameConfig.ITEM_HEIGHT;
        E.instance.setNoticeParentPos(this.curLevelMaxCol);
    };
    public loadDisplayMapData(e) {
        this.displayMapInfo = e;
        for (var t = 0, a = 0, o = -1, i = true, r = true, n = 0; n < this.displayMapInfo.length; n++) {
            for (var l = false, s = 0; s < this.displayMapInfo[n].length; s++) if (this.displayMapInfo[n][s] == h.DisplayMapType.Normal) {
                l = true;
                i = false;
                o < 0 && (o = n);
            }
            !l && i && t++;
        }
        for (n = this.displayMapInfo.length - 1; n >= 0; n--) {
            for (l = false, s = 0; s < this.displayMapInfo[n].length; s++) if (this.displayMapInfo[n][s] == h.DisplayMapType.Normal) {
                l = true;
                r = false;
                o < 0 && (o = n);
            }
            !l && r && a++;
        }
        this.ui.MatchGameCenter.x = ((t + a) / 2 - o) * MatchGameConfig.ITEM_WIDTH - 350 + 39;
    };
    public checkDisplayMapInfo() {
        s.log("this.displayMapInfo---", this.displayMapInfo);
        for (var e = 0; e < this.dropInfo.length; e++) {
            var t = this.dropInfo[e].startIndexY;
            if (!(t < 0)) for (var a = 0; a <= t; a++) this.displayMapInfo[e][a] == h.DisplayMapType.EmptyDrop && (this.displayMapInfo[e][a] = h.DisplayMapType.EmptyBlock);
        }
        s.log("this.displayMapInfo---", this.displayMapInfo);
    };
    public loadMiddleData(e) {
        for (var t = 0; t < this._map_row; t++) {
            for (var a = 0; a < this._map_col; a++) {
                this.displayMapInfo[t][a] === h.DisplayMapType.Normal && this.createMiddleSquareItem(t, a, e[t][a]);
            }
        }
        this.setBottomArr();
        this.setBottomMapInfo();
        this.checkDisplayMapInfo();
    };
    public setBottomMapInfo() {
        this.dropInfo = [];
        for (var e = 0; e < this._map_row; e++) {
            for (var t = [], a = -1, o = 0, i = false, r = -1, n = 0; n < this._map_col; n++) if (this.displayMapInfo[e][n] == h.DisplayMapType.Normal) {
                if (!i) {
                    i = true;
                    t.push(n - 1);
                }
                r < 0 && (r = n);
            }
            if (t.length) {
                for (n = 0; n < this._map_col; n++) if (this.displayMapInfo[e][n] == h.DisplayMapType.EmptyBlock) -1 == t.indexOf(n) && t.push(n); else if (this.displayMapInfo[e][n] == h.DisplayMapType.Normal) {
                    o += 1;
                    a = n;
                }
                this.dropInfo.push({
                    dropIndexArr: t,
                    endIndexY: a,
                    squareCount: o,
                    startIndexY: r
                });
            } else this.dropInfo.push({
                dropIndexArr: [],
                endIndexY: -1,
                squareCount: 0,
                startIndexY: -1
            });
        }
        s.log(" this.dropInfo=====", this.dropInfo);
    };
    public loadTopData(e) {
        for (var t = 0; t < this._map_row; t++) for (var a = 0; a < this._map_col; a++) this.displayMapInfo[t][a] === h.DisplayMapType.Normal && this.createTopSquareItem(t, a, e[t][a]);
    };
    public createBottomSquareItem(e, t, a) {
        var o = a.split("-"), i = o[0], r = o[1];
        i == h.SquareType.ObstacleItem && this.createObstacleItem(e, t, r, LayerType.Lowest);
    };
    public createMiddleSquareItem(e, t, a, o?) {
        void 0 === o && (o = false);
        if (!g.middleSquareGrid[e][t]) {
            var i, r = a.split("-"), n = r[0], l = r[1];
            n == h.SquareType.MatchItem ? i = this.createMatchItem(e, t, Number(l), o) : n == h.SquareType.PropItem ? i = this.createPropItem(e, t, Number(l), o) : n == h.SquareType.ObstacleItem ? i = this.createObstacleItem(e, t, l, LayerType.Middle, o) : n == h.SquareType.Null ? i = s.log("---该地点为空--N--", e, t) : n == h.SquareType.EMPTY ? i = s.log("---该地点为空--E--", e, t) : s.log("---配表报错--", n, l);
            return i;
        }
    };
    public createTopSquareItem(e, t, a) {
        var o = a.split("-"), i = o[0], r = o[1];
        i == h.SquareType.ObstacleItem && this.createObstacleItem(e, t, r, LayerType.Top);
    };
    public createMatchItem(e, t, a, o) {
        void 0 === o && (o = false);
        g.middleSquareGrid[e][t] && s.log("到这就对了~此处有其他方块", e, t, g.middleSquareGrid[e][t]);
        var i = v.createMatchItem(e, t, a, o);
        this.displayMapInfo[e][t] && g.setMiddleSquareGrid(e, t, i);
        return i;
    };
    public createObstacleItem(e, t, a, o, i?) {
        void 0 === i && (i = false);
        var r = this.getGridWithLayout(o);
        r[e] && r[e][t] && s.log("到这就对了~此处有其他方块 创建障碍物报错-----", e, t, a, o);
        var n = null;
        i && o == LayerType.Middle && (n = this.middleMaskParents);
        return b.createObstacleItem(e, t, a, o, n);
    };
    public createPropItem(e, t, a, o) {
        void 0 === o && (o = false);
        if (!g.middleSquareGrid[e][t]) {
            var i = C.createPropItem(e, t, a);
            this.displayMapInfo[e][t] && g.setMiddleSquareGrid(e, t, i);
            return i;
        }
        s.log("到这就对了~此处有其他方块 ", e, t, a);
    };
    public recoverSquare(e) {
        if (e) switch (e.squareType) {
            case h.SquareType.Null:
                break;

            case h.SquareType.MatchItem:
                v.destoryMatchItem(e.node);
                break;

            case h.SquareType.ObstacleItem:
                b.destoryObstacleItem(e.node);
                break;

            case h.SquareType.PropItem:
                C.destoryPropItem(e.node);
                break;

            default:
                s.log("---配表报错--", e);
        }
    };
    public recoverSquares(e) {
        var t = [], a = [], o = [];
        e.forEach((e) => {
            switch (e.squareType) {
                case h.SquareType.Null:
                    break;

                case h.SquareType.MatchItem:
                    a.push(e.node);
                    break;

                case h.SquareType.ObstacleItem:
                    t.push(e.node);
                    break;

                case h.SquareType.PropItem:
                    o.push(e.node);
                    break;

                default:
                    s.log("---配表报错--", e);
            }
        });
        t.length && b.destoryObstacleItems(t);
        a.length && v.destoryMatchItems(a);
        o.length && C.destoryPropItems(o);
    };
    public getMiddleSquareType(e, t) {
        var a = g.middleSquareGrid[e][t];
        if (!a) {
            s.log("找不到方块", e, t);
            return null;
        }
        return a;
    };
    public getBottomSquareType(e, t) {
        var a = g.middleSquareGrid[e][t];
        if (!a) {
            s.log("找不到方块", e, t);
            return null;
        }
        return a;
    };
    public setBottomArr() {
        this._bottomArr = [];
        for (var e = -1, t = -1, a = -1, o = 0; o < this._map_row; o++) {
            for (var i = 0, r = -1, n = -1, l = 0; l < this._map_col; l++) if (g.middleSquareGrid[o][l]) {
                -1 == r && (r = l);
                n = l;
                i++;
                (-1 == t || l < t) && (t = l);
                (-1 == a || l > a) && (a = l);
            }
            this._bottomArr[o] = {
                index1: r,
                count: i,
                index2: n
            };
            -1 !== r && -1 == e && (e = o);
        }
        s.log("设置补充棋子的地图范围,_bottomArr----", this._bottomArr);
    };
    public existSquareUpperLayer(e, t, a) {
        if (a == LayerType.Top) return true;
        for (var o = a + 1; o <= LayerType.Top; o++) {
            var i = this.getGridWithLayout(o);
            if (i && i[e] && i[e][t]) return true;
        }
        return false;
    };
    public getGridWithLayout(e) {
        return e == LayerType.Lowest ? this.bottomSquareGrid : e == LayerType.Middle ? g.middleSquareGrid : e == LayerType.Top ? this.topSquareGrid : null;
    };
    public addMiddleSquareGrid(e, t, a) {
        if (g.middleSquareGrid[e]) {
            g.middleSquareGrid[e][t] && s.log("中层数据已存在", e, t);
            g.setMiddleSquareGrid(e, t, a);
        }
        s.log("中层数据不存在", e, t, a);
    };
    public getMiddleSquareGrid() {
        return g.middleSquareGrid;
    };
    public addTopSquareGrid(e, t, a) {
        if (this.topSquareGrid[e]) {
            this.topSquareGrid[e][t] && s.log("高层数据已存在", e, t);
            this.topSquareGrid[e][t] = a;
        }
        s.log("高层数据不存在", e, t, a);
    };
    public delateTopSquareGrid(e, t) {
        if (this.topSquareGrid[e]) {
            this.topSquareGrid[e][t] || s.log("高层数据删除不存在", e, t);
            this.topSquareGrid[e][t] = null;
        }
    };
    public getTopSquareGrid() {
        return this.topSquareGrid;
    };
    public addBottomSquareGrid(e, t, a) {
        if (this.bottomSquareGrid[e]) {
            this.bottomSquareGrid[e][t] && s.log("底层数据已存在", e, t);
            this.bottomSquareGrid[e][t] = a;
        }
        s.log("底层数据不存在", e, t, a);
    };
    public delateBottomSquareGrid(e, t) {
        if (this.bottomSquareGrid[e]) {
            this.bottomSquareGrid[e][t] || s.log("底层数据删除不存在", e, t);
            this.bottomSquareGrid[e][t] = null;
        }
    };
    public getBottomSquareGrid() {
        return this.bottomSquareGrid;
    };
    public destoryItem(e) {
        if (e && e.node && !y.getSquareDestoryStatus(e)) if (y.getSquareChangeStatus(e)) s.error("方块销毁时正在交换中", e); else {
            var t = e._row, o = e._col;
            if (e.layerType == LayerType.Middle) {
                if (g.middleSquareGrid[t] && g.middleSquareGrid[t][o]) {
                    y.destorySquare(e);
                    MatchGameCenterCtrl._instance.recoverSquare(e);
                    g.middleSquareGrid[t][o] = null;
                    return;
                }
            } else if (e.layerType == LayerType.Top) {
                if (this.topSquareGrid[t] && this.topSquareGrid[t][o]) {
                    y.destorySquare(e);
                    MatchGameCenterCtrl._instance.recoverSquare(e);
                    this.topSquareGrid[t][o] = null;
                    return;
                }
            } else if (e.layerType == LayerType.Lowest && this.bottomSquareGrid[t] && this.bottomSquareGrid[t][o]) {
                y.destorySquare(e);
                MatchGameCenterCtrl._instance.recoverSquare(e);
                this.bottomSquareGrid[t][o] = null;
                return;
            }
            s.log("销毁方块报错", e);
        }
    };
    public getMiddleGridItem(e, t) {
        var o = MatchGameCenterCtrl._instance.displayMapInfo;
        if (o[e] && o[e][t] && o[e][t] !== h.DisplayMapType.Normal) return null;
        if (!g.middleSquareGrid[e] || !g.middleSquareGrid[e][t]) return null;
        if (!g.middleSquareGrid[e][t].node) {
            s.log("找不到中间元素的节点", e, t);
            return null;
        }
        return g.middleSquareGrid[e][t];
    };
    public getNormalMiddleGridItem(e, t) {
        var o = MatchGameCenterCtrl._instance.displayMapInfo;
        if (o[e] && o[e][t] && o[e][t] !== h.DisplayMapType.Normal) return null;
        if (!g.middleSquareGrid[e] || !g.middleSquareGrid[e][t]) return null;
        if (!g.middleSquareGrid[e][t].node) {
            s.log("找不到中间元素的节点", e, t);
            return null;
        }
        return g.middleSquareGrid[e][t];
    };
    public getMatchItem(e, t) {
        var a = this.getMiddleGridItem(e, t);
        return a && a.squareType == h.SquareType.MatchItem ? a : null;
    };
    public getPropItem(e, t) {
        var a = this.getMiddleGridItem(e, t);
        return a && a.squareType == h.SquareType.PropItem ? a : null;
    };
    public getNormalPropItem(e, t) {
        var a = this.getNormalMiddleGridItem(e, t);
        return a && a.squareType == h.SquareType.PropItem ? a : null;
    };
    public getReadyDestoryPropItem(e, t) {
        var a = this.getNormalMiddleGridItem(e, t);
        return a && a.squareType == h.SquareType.PropItem ? a : null;
    };
}